package com.wang.nowcoder.service;

import com.wang.nowcoder.dao.DiscussPostMapper;
import com.wang.nowcoder.pojo.DiscussPost;
import com.wang.nowcoder.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

/**
 * @author wang
 * @ClassName DiscussPostService
 * @Description TODO
 * @date 2021/12/14 11:41
 * @Version 1.0
 */
@Service
public class DiscussPostService {

    @Autowired
    private DiscussPostMapper discussPostMapper;

    @Autowired
    private SensitiveFilter sensitiveFilter;
    /**
     * 遍历数据库，对信息进行查询并实现分页
     * @param userId
     * @param offset
     * @param limit
     * @return
     */
    public List<DiscussPost> selectDiscussPost(int userId, int offset, int limit){
        return discussPostMapper.selectDiscussPost(userId, offset, limit);
    }

    /**
     * 查询评论数量
     * @param userId
     * @return
     */
    public int selectDiscussPostRows(int userId){
        return discussPostMapper.selectDiscussPostRows(userId);
    }

    /**
     * 发布帖子并过滤敏感词
     * @param discussPost
     * @return
     */
    public int addDiscussPost(DiscussPost discussPost){
        if (discussPost==null){
            throw new IllegalArgumentException("参数不能为空");
        }

        //转义HTML标记
        discussPost.setTitle(HtmlUtils.htmlEscape(discussPost.getTitle()));
        discussPost.setContent(HtmlUtils.htmlEscape(discussPost.getContent()));

        //过滤敏感词
        discussPost.setTitle(sensitiveFilter.filter(discussPost.getTitle()));
        discussPost.setContent(sensitiveFilter.filter(discussPost.getContent()));

        return discussPostMapper.insertDiscussPost(discussPost);
    }

    public DiscussPost selectDiscussPostByID(int id){
        return discussPostMapper.selectDiscussPostByID(id);
    }

    public int updateCommentCount(int id,int commentCount){
        return discussPostMapper.updateCommentCount(id, commentCount);
    }

}
